import { Component, OnInit } from '@angular/core';
import { USER_ROLE_OPTION } from '../../../constant/dictionary';
import { MyModalHelper } from '../../../core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { AgentService } from '@core/routes-services/agent.service';
import { DistrictService } from '@core/routes-services/district.service';
import { AgentLevelService } from '@core/routes-services/agent-level.service';

@Component({
  selector: 'app--preformance-list',
  templateUrl: './preformance-list.component.html',
})
export class PreformanceListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  levelId:any;
  parentId:any;
  districtId:any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  userRoleOption: any [] = USER_ROLE_OPTION;

  levelOptions: any [] = [];
  allAgentOption:any [] = [];
  districtOption:any [] = [];

  constructor(private message: NzMessageService,
              private agentService:AgentService,
              private agentLevelService: AgentLevelService,
              private districtService:DistrictService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initDistrice();
    this.initAllAgent();
    this.initLevelOptions();
    this.initPage();
  }
  initDistrice(){
    this.districtService.getAll().then((res) => {
      console.log(res )
      if (res.status.toString().startsWith("20")) {
        this.districtOption = res.data;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });

  }

  initLevelOptions() {
    this.isLoading = true;
    this.agentLevelService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.levelOptions = res.data.agentLevelsList;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
  initAllAgent(){
    this.agentService.getAll().then((res) => {
      console.log(res )
      if (res.status.toString().startsWith("20")) {
        this.allAgentOption = res.data.agents;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }
  initPage() {
    this.isLoading = true;
    this.agentService.pagination(this.page, this.keyword, this.levelId, this.parentId,this.districtId).then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.page = res.data;
        console.log(this.page);
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    this.message.success("todo");
    // let temp: boolean;
    // temp = myValidNull(entity.id);
    // const bak = {};
    // Object.assign(bak, entity);
    // this.modalHelper.open(UserEditComponent, { entity: bak }, 500, {
    //   nzTitle: '用户信息',
    // }).subscribe(result => {
    //   if(result){
    //     temp ? this.search() : this.changePageNo(this.page.number);
    //   }
    // }, (error) => {
    //   console.error('弹窗回调出错：', error);
    // });
  };


}
